﻿Imports Microsoft.VisualBasic

Namespace MyApp
    Namespace Network
        Public Class Buffer

            Private db As mDB = mDB.GetDB

            Public Property oauth2 As WebApi.Oauth.Oauth2

            Public Sub New()

            End Sub

            Public Sub New(oauth2 As WebApi.Oauth.Oauth2)
                Me.oauth2 = oauth2

            End Sub

            Public Function RedirectAuth() As String

                Return oauth2.GetDialog("https://bufferapp.com/oauth2/authorize",
                    "") 'profile

            End Function



            Public Sub AddLogin(code As String)
                Dim CurrUser = MyApp.GetUser(False)

                'oauth2.access_token = access_token
                oauth2.code = code

                oauth2.ConfirmingIdentity("https://api.bufferapp.com/1/oauth2/token.json", "POST", "JSON")


                Dim tc = New WebApi.Buffer.BufferClient(oauth2)

                Dim CurrPeople = tc.GetProfiles

                For Each prof In CurrPeople

                    '--------------------------------------------
                    ' creo account

                    Dim Acc = Me.UpdateProfile(prof)

                    '--------------------------------------------
                    ' creo account_auth

                    Dim AccAuth = Acc.Account_Auth ' CurrUser.tab_acc Tab_UserAccount.BY.User_Account.Account_Id(acc.Id).Limit(1).ToObj
                    If AccAuth Is Nothing Then
                        AccAuth = New mDB.Account_Auth
                        db.TAB.Account_Auth.InsertOnSubmit(AccAuth)
                    End If

                    AccAuth.Account_Id = Acc.id
                    AccAuth.Network_Id = MyApp.Network.AccType.BUFFER
                    AccAuth.Oauth_Consumer_Key = oauth2.client_id
                    AccAuth.Oauth_Consumer_Secret = oauth2.client_secret
                    AccAuth.Oauth_Token = oauth2.access_token
                    AccAuth.Oauth_Token_Secret = oauth2.refresh_token

                    AccAuth.Error_Account_Last_Date = Date.MinValue
                    AccAuth.Error_Account_Count = 0
                    AccAuth.Disabled = 0

                    '--------------------------------------------
                    ' creo user_account
                    Dim UserAcc = CurrUser.Tab_User_Account(Acc.id).ToObj
                    If UserAcc Is Nothing Then
                        UserAcc = New mDB.User_Account
                        db.TAB.User_Account.InsertOnSubmit(UserAcc)
                    End If

                    UserAcc.User_Id = CurrUser.Id
                    UserAcc.Account_Id = Acc.id

                    db.SubmitChanges()

                Next

            End Sub
            'Public Sub RefreshLogin()

            '    'oauth2.access_token = access_token

            '    oauth2.RefreshIdentity("https://accounts.google.com/o/oauth2/token", "POST", "JSON")

            '    Dim tc = New WebApi.Google.GoogleClient(oauth2)

            '    Dim CurrPeople = tc.GetPeople("me")
            '    Dim Acc = Me.UpdateAccountPeople(CurrPeople)

            '    '--------------------------------------------
            '    ' creo account_auth

            '    Dim AccAuth = Acc.Account_Auth
            '    AccAuth.Oauth_Token = oauth2.access_token

            '    AccAuth.Error_Account_Last_Date = Date.MinValue
            '    AccAuth.Error_Account_Count = 0
            '    AccAuth.Disabled = 0

            'End Sub


            Public Function UpdateProfile(tu As WebApi.Buffer.Profile) As mDB.Account

                '--------------------------------------------
                ' creo account
                Dim Acc = mDB.Account.GetForUpdate(tu.id, AccType.BUFFER)


                Acc.Fullname = "[" & tu.service & "] - " & tu.formatted_username
                Acc.Username = tu.service_username
                Acc.Avatar = tu.avatar
                'If tu.location IsNot Nothing Then
                '    Acc.Location = tu.location.name
                'End If
                'Acc.Description = MyApp.ToUTF8DB(tu.headline)
                Acc.Url = ""

                Acc.Account_Add_Info.Profile_Update = Now
                'Acc.Account_Update.Follower_Count = tu.numconnections

                db.SubmitChanges()

                Return Acc

            End Function

            Public Function SendPost(CurrAccount As mDB.Account, Post As mDB.Task_Post) As WebApi.Buffer.Update

                Dim p As WebApi.Buffer.Update = Nothing
                Dim buf = New WebApi.Buffer.BufferClient(Me.oauth2)
                'tc.AutoProc = Task.TaskType.SEND_POSTS

                MyApp.WriteProcLog(SF("(tab)(tab)send post : '{0}', to: {1}, type: {2}", Post.Post_Text, Post.To_Account_Username, Post.Post_Type_Id), Task.TaskType.SEND_POSTS)

                Select Case DirectCast(Post.Post_Type_Id, mDB.Task_Post.Type)
                    Case mDB.Task_Post.Type.NORMAL

                        If Post.Media_Id = 0 Then
                            p = buf.PostUpdateCreate(CurrAccount.Account_Code, Post.Post_Text).updates(0)
                        Else
                            'http://labs.glouk.it/app/MediaUpload.aspx?id=55
                            Dim Media As New Dictionary(Of String, String)
                            Media.Add("photo", Post.Media.PublicUrl)
                            Media.Add("thumbnail", Post.Media.PublicUrl)
                            'Media.Add("photo", "http://labs.glouk.it/app/MediaUpload.aspx?id=55")
                            'Media.Add("thumbnail", "http://labs.glouk.it/app/MediaUpload.aspx?id=55")
                            p = buf.PostUpdateCreate(CurrAccount.Account_Code, Post.Post_Text, Media).updates(0)

                        End If

                    Case mDB.Task_Post.Type.PRIVATE

                    Case mDB.Task_Post.Type.SHARE

                End Select

                Return p
            End Function
            'Public Function DownloadAccountFriends(Acc As mDB.Account) As Boolean
            '    Dim tc = New WebApi.Google.GoogleClient(oauth2)

            '    'prendo l'ulmito cursor
            '    Dim Cursor = Acc.Account_Update.Friend_Last_Code


            '    Do
            '        'scarico i friends
            '        Dim Foll = tc.GetPeopleList(Acc.Account_Code, Cursor)

            '        MyApp.WriteProcLog(SF("(tab)(tab)update Friends: {1}", Acc.Fullname, Foll.items.Count), Task.TaskType.UPDATE_ACCOUNTS)

            '        For Each f In Foll.items

            '            Acc.AddAccountLink(mDB.Account_Link.Type.FRIENDS, f.id, AccType.GOOGLE_PEOPLE)
            '            Me.UpdateAccountPeople(f)

            '        Next
            '        Cursor = Foll.nextPageToken

            '        '-------------------------------------------------------------
            '        'aggiona il link_update
            '        Acc.Account_Update.Friend_Last_Code = Cursor

            '        db.SubmitChanges()

            '    Loop Until Cursor = ""

            '    Return Cursor = ""




            'End Function

        End Class


    End Namespace
End Namespace
